package cn.estore.dao;

import cn.estore.entity.OrderEntity;
import cn.estore.util.DBConnection;
import java.sql.*;
import java.util.*;
import cn.estore.dao.OrderItemDao;

//订单表的操作
public class OrderDao {
	private Connection connection = null; // 定义连接的对象
	private PreparedStatement ps = null; // 定义数据库操作的语句对象
	private DBConnection jdbc = null; // 定义数据库连接对象

	public OrderDao() {
		jdbc = new DBConnection();
		connection = jdbc.connection; // 利用构造方法取得数据库连接
	}

	// 添加订单
	public boolean insertOrder(OrderEntity form) {
		try {
			ps = connection.prepareStatement("insert into tb_order values (?,?,?,?,?,?,?,?,?,now(),?)");
			ps.setString(1, form.getOrderId());
			ps.setString(2, form.getName());
			ps.setString(3, form.getRealName());
			ps.setString(4, form.getAddress());
			ps.setString(5, form.getMobile());
			ps.setFloat(6, Float.valueOf(form.getTotalPrice()));
			ps.setString(7, form.getDeliveryMethod());
			ps.setString(8, form.getMemo());
			ps.setBoolean(9, form.getDeliverySign());
			ps.setString(10, form.getPaymentmode());
			ps.executeUpdate();
			ps.close();
			return true;
		} catch (SQLException ex) {
			System.out.println("数据库访问失败");
			System.out.println(ex.getMessage());
			return false;
		}
	}
	/* 前台会员订单查询(按会员名查询)
	 * 输入: 会员名
	 * 输出：该会员对应的所有订单集合List[e]，其中的e是OrderEntity对象
	 */
	public List selectOrderByName(String name) {
		List list = new ArrayList();
		try {
			ps = connection.prepareStatement("select * from tb_order where name=?");
			ps.setString(1, name);
			ResultSet rs = ps.executeQuery();// 获取指定姓名的所有订单资料
			while (rs.next()) {// 循环将rs中数据存至order对象中
				OrderEntity order = new OrderEntity();
				order.setOrderId(rs.getString(1));
				order.setName(rs.getString(2));
				order.setRealName(rs.getString(3));
				order.setAddress(rs.getString(4));
				order.setMobile(rs.getString(5));
				order.setTotalPrice(Float.valueOf(rs.getString(6)));
				order.setDeliveryMethod(rs.getString(7));
				order.setMemo(rs.getString(8));
				order.setDeliverySign(rs.getBoolean(9));
				order.setCreateTime(rs.getString(10));
				order.setPaymentmode(rs.getString(11));
				list.add(order);// 将order对象存进list
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return list;
	}

	// 以订单号为条件查询信息
	public OrderEntity selectOrderByNumber(String orderId) {
		OrderEntity order = null;
		try {
			ps = connection.prepareStatement("select * from tb_order where order_id=?");
			ps.setString(1, orderId);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				order = new OrderEntity();
				order.setOrderId(rs.getString(1));
				order.setName(rs.getString(2));
				order.setRealName(rs.getString(3));
				order.setAddress(rs.getString(4));
				order.setMobile(rs.getString(5));
				order.setTotalPrice(Float.valueOf(rs.getString(6)));
				order.setDeliveryMethod(rs.getString(7));
				String memo = null;
				try {
					memo = rs.getString(8);
				} catch (SQLException e) {
					// doNothing
				}
				order.setMemo(memo);
				order.setDeliverySign(rs.getBoolean(9));
				order.setCreateTime(rs.getString(10));
				order.setPaymentmode(rs.getString(11));
			}
		} catch (SQLException ex) {
		}
		return order;
	}

	// 以会员名和出货标志为条件查询信息
	public List selectOrderByNameAndDeliverySign(String name, Boolean sign) {
		List list = new ArrayList();// 准备存放已/未出货订单
		OrderEntity order = null;
		try {// 按姓名+已出或未出货标志查询订单信息
			ps = connection.prepareStatement(
					"select * from tb_order " + "where name=? and delivery_sign=? order by  order_id DESC");
			ps.setString(1, name);// 为两变量填充数据
			ps.setBoolean(2, sign);
			ResultSet rs = ps.executeQuery();// 执行sql查询
			while (rs.next()) {// 依次读取数据集，存放至order临时对象中
				order = new OrderEntity();
				order.setOrderId(rs.getString(1));
				order.setName(rs.getString(2));
				order.setRealName(rs.getString(3));
				order.setAddress(rs.getString(4));
				order.setMobile(rs.getString(5));
				order.setTotalPrice(Float.valueOf(rs.getString(6)));
				order.setDeliveryMethod(rs.getString(7));
				order.setMemo(rs.getString(8));
				order.setDeliverySign(rs.getBoolean(9));
				order.setCreateTime(rs.getString(10));
				order.setPaymentmode(rs.getString(11));
				list.add(order);// 将一订单存放到list中
			}
		} catch (SQLException ex) {
		}
		return list;// 返回查询已出货或未出货订单集合list
	}
	/* 管理员查询会员所有订单查询
	 * 输入: 无
	 * 输出：商城所有订单集合List[e]，其中的e是OrderEntity对象
	 */
	public List selectAllOrder() {
		List list = new ArrayList();
		try {
			ps = connection.prepareStatement("select * from tb_order");
			ResultSet rs = ps.executeQuery();// 获取所有订单资料
			while (rs.next()) {// 循环将rs中的数据存至order对象中
				OrderEntity order = new OrderEntity();
				order.setOrderId(rs.getString(1));
				order.setName(rs.getString(2));
				order.setRealName(rs.getString(3));
				order.setAddress(rs.getString(4));
				order.setMobile(rs.getString(5));
				order.setTotalPrice(Float.valueOf(rs.getString(6)));
				order.setDeliveryMethod(rs.getString(7));
				order.setMemo(rs.getString(8));
				order.setDeliverySign(rs.getBoolean(9));
				order.setCreateTime(rs.getString(10));
				order.setPaymentmode(rs.getString(11));
				list.add(order);// 将order对象存进list
			}
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return list;
	}
	// 出货操作
	public void updateSignOrder(String orderId) {
		try {
			ps = connection.prepareStatement(
	"update tb_order set delivery_sign='true' where order_id=?");
			ps.setString(1, orderId);
			ps.executeUpdate();
			ps.close();
			} catch (SQLException ex) {
	ex.printStackTrace();
		             }
	}

	// 删除订单(删除订单前必须先删除订单明细)
	public boolean deleteOrder(String orderId) {
	//删除订单明细
		OrderItemDao orderItem=new OrderItemDao();
		orderItem.deleteOrderItems(orderId);
		try {
			ps = connection.prepareStatement(
	"delete from tb_order where order_id=?");
	 
			ps.setString(1, orderId);
	       //删除订单
			ps.executeUpdate();
			ps.close();
			return true;
			} catch (SQLException ex) {
				return false;
		}
	}

}
